home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / system-config-printer / timedops.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  7.2 KB  |  192 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import gobject
  5. import gtk
  6. import subprocess
  7. import threading
  8. from gettext import gettext as _
  9. from debug import *
  10.  
  11. class OperationCanceled(RuntimeError):
  12.     pass
  13.  
  14.  
  15. class Timed:
  16.     
  17.     def run(self):
  18.         pass
  19.  
  20.     
  21.     def cancel(self):
  22.         pass
  23.  
  24.  
  25.  
  26. class TimedSubprocess(Timed):
  27.     
  28.     def __init__(self, timeout = 60000, parent = None, show_dialog = True, **args):
  29.         self.subp = subprocess.Popen(**args)
  30.         self.output = dict()
  31.         self.io_source = []
  32.         self.watchers = 2
  33.         self.timeout = timeout
  34.         self.parent = parent
  35.         self.show_dialog = show_dialog
  36.         for f in [
  37.             self.subp.stdout,
  38.             self.subp.stderr]:
  39.             source = gobject.io_add_watch(f, gobject.IO_IN | gobject.IO_HUP | gobject.IO_ERR, self.watcher)
  40.             self.io_source.append(source)
  41.         
  42.         self.wait_window = None
  43.  
  44.     
  45.     def run(self):
  46.         if self.show_dialog:
  47.             self.wait_source = gobject.timeout_add(1000, self.show_wait_window)
  48.         
  49.         self.timeout_source = gobject.timeout_add(self.timeout, self.do_timeout)
  50.         gtk.main()
  51.         gobject.source_remove(self.timeout_source)
  52.         if self.show_dialog:
  53.             gobject.source_remove(self.wait_source)
  54.         
  55.         for source in self.io_source:
  56.             gobject.source_remove(source)
  57.         
  58.         if self.wait_window != None:
  59.             self.wait_window.destroy()
  60.         
  61.         return (self.output.get(self.subp.stdout, '').split('\n'), self.output.get(self.subp.stderr, '').split('\n'), self.subp.poll())
  62.  
  63.     
  64.     def do_timeout(self):
  65.         gtk.main_quit()
  66.         return False
  67.  
  68.     
  69.     def watcher(self, source, condition):
  70.         if condition & gobject.IO_IN:
  71.             buffer = self.output.get(source, '')
  72.             buffer += source.read()
  73.             self.output[source] = buffer
  74.         
  75.         return True
  76.  
  77.     
  78.     def show_wait_window(self):
  79.         wait = gtk.MessageDialog(self.parent, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, gtk.BUTTONS_CANCEL, _('Please wait'))
  80.         wait.connect('delete_event', (lambda : False))
  81.         wait.connect('response', self.wait_window_response)
  82.         if self.parent:
  83.             wait.set_transient_for(self.parent)
  84.         
  85.         wait.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
  86.         wait.format_secondary_text(_('Gathering information'))
  87.         wait.show_all()
  88.         self.wait_window = wait
  89.         return False
  90.  
  91.     
  92.     def wait_window_response(self, dialog, response):
  93.         if response == gtk.RESPONSE_CANCEL:
  94.             self.cancel()
  95.         
  96.  
  97.     
  98.     def cancel(self):
  99.         if self.watchers > 0:
  100.             debugprint('Command canceled')
  101.             gtk.main_quit()
  102.             self.watchers = 0
  103.         
  104.  
  105.  
  106.  
  107. class OperationThread(threading.Thread):
  108.     
  109.     def __init__(self, target = None, args = (), kwargs = { }):
  110.         threading.Thread.__init__(self)
  111.         self.setDaemon(True)
  112.         self.target = target
  113.         self.args = args
  114.         self.kwargs = kwargs
  115.         self.exception = None
  116.         self.result = None
  117.  
  118.     
  119.     def run(self):
  120.         
  121.         try:
  122.             debugprint('Calling %s' % self.target)
  123.             self.result = self.target(*self.args, **self.kwargs)
  124.             debugprint('Done')
  125.         except Exception:
  126.             e = None
  127.             debugprint('Caught exception %s' % e)
  128.             self.exception = e
  129.  
  130.  
  131.     
  132.     def collect_result(self):
  133.         if self.isAlive():
  134.             raise OperationCanceled()
  135.         self.isAlive()
  136.         if self.exception:
  137.             raise self.exception
  138.         self.exception
  139.         return self.result
  140.  
  141.  
  142.  
  143. class TimedOperation(Timed):
  144.     
  145.     def __init__(self, target, args = (), kwargs = { }, parent = None, show_dialog = False):
  146.         self.wait_window = None
  147.         self.parent = parent
  148.         self.show_dialog = show_dialog
  149.         self.thread = OperationThread(target = target, args = args, kwargs = kwargs)
  150.         self.thread.start()
  151.  
  152.     
  153.     def run(self):
  154.         if self.show_dialog:
  155.             wait = gtk.MessageDialog(self.parent, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, gtk.BUTTONS_CANCEL, _('Please wait'))
  156.             wait.connect('delete_event', (lambda : False))
  157.             wait.connect('response', self.wait_window_response)
  158.             if self.parent:
  159.                 wait.set_transient_for(self.parent)
  160.             
  161.             wait.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
  162.             wait.format_secondary_text(_('Gathering information'))
  163.             wait.show_all()
  164.         
  165.         self.timeout_source = gobject.timeout_add(50, self.check_thread)
  166.         gtk.main()
  167.         gobject.source_remove(self.timeout_source)
  168.         if self.show_dialog:
  169.             wait.destroy()
  170.         
  171.         return self.thread.collect_result()
  172.  
  173.     
  174.     def check_thread(self):
  175.         if self.thread.isAlive():
  176.             return True
  177.         gtk.main_quit()
  178.         return False
  179.  
  180.     
  181.     def wait_window_response(self, dialog, response):
  182.         if response == gtk.RESPONSE_CANCEL:
  183.             self.cancel()
  184.         
  185.  
  186.     
  187.     def cancel(self):
  188.         debugprint('Command canceled')
  189.         gtk.main_quit()
  190.  
  191.  
  192.